<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller as BaseController;
use App\Repository\Sms\SmsConfRepository;
use Illuminate\Http\Request;

// 分销商
class MessageSendConfController extends BaseController
{
    public function __construct(SmsConfRepository $model)
    {
        $this->repoist = $model;
    }

    public function store(Request $request)
    {
        return $this->repoist->add($request);
    }

    public function index(Request $request)
    {
        $where = [];
        if (!empty($request->get('name'))) {
            $where['like'] = ['sms_tmp_name' => $request->get('name')];
        }
        if (!empty($request->get('order_status'))) {
            $where['like'] = ['order_status_desc' => $request->get('order_status')];
        }
        if (!empty($request->get('product_name'))) {
            $where['like'] = ['product_name' => $request->get('product_name')];
        }
        if (!empty($request->get('product_id'))) {
            $where['where'] = ['product_id' => $request->get('product_id')];
        }
        if (!empty($request->get('pageNum'))) {
            $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
            $where['limit']['number'] = $request->get('pageSize');
        }

        if ($request->user->role_id != 1) {
            $where['where']['shop_id'] = $request->user->shop_id;
        }
        $where['select'] = ['sms_tmp_name', 'dist_name', 'dist_id', 'sms_tmp_id', 'order_status', 'order_status_desc', 'is_phone', 'goods_name', 'delay', 'goods_id', 'shop_id', 'id', 'created_at', 'updated_at', 'status', 'product_id', 'product_name'];
        $data = $this->repoist->index($where);
        return $data;
    }

    public function save(Request $request)
    {
        if (!$request->get('id')) {
            return $this->returnMsg(1, '请传递id参数');
        }
        $where = ['id' => $request->get('id'), 'shop_id' => $request->user->shop_id];
        return $this->repoist->edit($where, $request);
    }

    public function show(Request $request, $id)
    {
        $where = ['id' => $id, 'shop_id' => $request->user->shop_id];
        //查询一条数据
        $return = $this->repoist->getOne($where, ['*']);
        if (!empty($return)) {
            $return['status'] = (int)$return->status;
            $return['is_phone'] = (int)$return->is_phone;
            if ($return['dist_id'] == $return['shop_id']) {
                $return['dist_id'] = '';
            }
        }

        return $this->returnMsg(200, '查询成功', $return);
    }

    public function listss(Request $request)
    {
        $where = [];
        if (!empty($request->get('name'))) {
            $where['where'] = ['name' => $request->get('name')];
        }
        $where['where']['shop_id'] = $request->user->shop_id;
        // var_dump($where);
        $return = $this->repoist->lists($where, 1, 99999);
        return $return;
    }

    public function destroy(Request $request, $id = 0)
    {
        $delId = $request->get('ids');
        if (empty($delId)) {
            return $this->returnMsg(10, '请传递参数');
        }

        return $this->repoist->del($request->all());
    }

    public function updateStatus(Request $request, $id)
    {
        $where = [
            'id' => $id,
            'shop_id' => $request->user->shop_id
        ];
        $update = [
            'status' => $request->get('status'),
        ];
        return $this->repoist->edit($where, $request);
    }
}
